package com.application.sistercityapplication.main;

import java.util.ArrayList;
import java.util.List;

import com.application.sistercityapplication.dto.NewsDto;
import com.application.sistercityapplication.json.JSONParserForGetList;
import com.application.sistercityapplication.news.NewsListAdapter;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.Toast;

@SuppressLint("ValidFragment")
public class NoticeAndNewsFragment extends Fragment {

	private ProgressBar pBar;
	private View v, v2;
	private LayoutInflater inflater2;
	private FrameLayout mainLayout;
	private int cId;
	private ListView lv;
	private ImageView menuToggle;
	private SlidingMenu sm;
	private String country_name;
	public NoticeAndNewsFragment(int country_id,SlidingMenu sm,String country_name) {
		this.cId = country_id;
		this.country_name = country_name;
		this.sm = sm;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		v = inflater.inflate(R.layout.new_activity_city_information_main,container, false);

		mainLayout = (FrameLayout) v.findViewById(R.id.frameForInflate);
		inflater2 = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		v2 = inflater2.inflate(R.layout.main_news_layout, null);
		ImageView back_icon = (ImageView) v2.findViewById(R.id.back_icon);
//		back_icon.setVisibility(View.INVISIBLE);
		menuToggle = (ImageView) v2.findViewById(R.id.menuToggle);
		menuToggle.setVisibility(View.INVISIBLE);
		pBar = (ProgressBar) v2.findViewById(R.id.progress);
		mainLayout.addView(v2);
		List<NewsDto> listNews = new ArrayList<NewsDto>();
		lv = (ListView) v2.findViewById(R.id.listNews);
		new DownLoadNewsTask(getActivity().getApplicationContext(),listNews,getActivity(),pBar).execute();
		menuToggle.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				sm.toggle();
			}
		});
		back_icon.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Fragment newContent = new MainPageFragment(cId,country_name,sm);
				switchFragment(newContent);
			}
		});
		return v;
	}
	private void switchFragment(Fragment fragment) {
		if (getActivity() == null)
			return;

		if (getActivity() instanceof NewDashBoardPageActivity) {
			NewDashBoardPageActivity fca = (NewDashBoardPageActivity) getActivity();
			fca.switchContent(fragment);
		}
	}

	public class DownLoadNewsTask extends AsyncTask<String, Void, List<NewsDto>> {
		ProgressDialog pDialog;
		private Context context;
		private List<NewsDto> listNews;
		public Activity _activity;
		private ProgressBar _resid;

		public DownLoadNewsTask(Context context, List<NewsDto> view,Activity act,ProgressBar resid) {
			super();
			this.context = context;
			this.listNews = view;
			this._activity = act;
			this._resid = resid;
		}

		@Override
		protected void onPreExecute() {
			super.onPreExecute();
//			pDialog = new ProgressDialog(MainHomePageViewActivity.this);
//			pDialog.setMessage("Loading Content...");
//			pDialog.show();
			_resid.setVisibility(View.VISIBLE);
		}

		@Override
		protected List<NewsDto> doInBackground(String... params) {
			JSONParserForGetList json = JSONParserForGetList.getInstance();
			listNews = json.getNews(cId+"");
			return listNews;
		}

		@Override
		protected void onPostExecute(List<NewsDto> result) {
			super.onPostExecute(result);
//			if (null != pDialog && pDialog.isShowing()) {
//				pDialog.dismiss();
//			}
			menuToggle.setVisibility(View.VISIBLE);
			_resid.setVisibility(View.GONE);
			if (result != null) {
				NewsListAdapter nla = new NewsListAdapter(listNews,getActivity().getApplicationContext(), getActivity());
				lv.setAdapter(nla);
			} else {
				(Toast.makeText(context, "An error occured.", Toast.LENGTH_LONG)).show();
			}

		}
	}
}
